/*
    This file is part of the substandard lisp implementation of Standard Lisp
    Copyright (C) 2011 Nicky Nickell

    substandard lisp is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

std_backquote = (a, env) ->
  form = a[0]

  if form.type isnt TYPE_PAIR
    return form
  else
    out = []
    for elm in list_to_array form
      if elm.type is TYPE_PAIR
        if elm.car.type is TYPE_ID
          if elm.car.value is 'unquote'
            out.push elm.cdr.car.evaluate(env)
            continue
          else if elm.car.value is 'unquotel'
            val = elm.cdr.car.evaluate(env)
            if val.type isnt TYPE_PAIR
              out.push '.'
              out.push val
            else
              out = out.concat list_to_array val
            continue
      out.push std_backquote([elm], env)

    return array_to_list out
      

/*---
name: backquote
arity: 1
rest: false
ftype: FTYPE_FEXPR

argument-types:
  - CLASS_ANY

argument-names:
  - FORM

doc: |
  explaining what backquote is / how it is used!

tests: |
  (setq l '(1 2 3))
  `(a b c) -> (a b c)
  `(a b c l) -> (a b c l)
  `(a b c ,l) -> (a b c (1 2 3))
  `(a b c ,@l) -> (a b c 1 2 3)
  `(a b c ,@1) -> (a b c . 1)
  `(a b c ,(plus 1 1)) -> (a b c 2)
  `(a b ,l c) -> (a b (1 2 3) c)
  `(a b ,@l c) -> (a b 1 2 3 c)
  `(a b c (d e ,l)) -> (a b c (d e (1 2 3)))
  `(a b c (d e ,@l)) -> (a b c (d e 1 2 3))
  `(a b (d e ,l) c) -> (a b (d e (1 2 3)) c)
  `(a b (d e ,@l) c) -> (a b (d e 1 2 3) c)
...*/
